package com.hj.ct.activity;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.inputmethod.InputMethodManager;
import android.widget.Toast;

import com.hj.ct.ActivityStackManager;
import com.hj.ct.App;
import com.hj.ct.logic.LoginLogic;
import com.hj.ct.logic.BaseLogic.OnHandlerResponse;
import com.hj.ct.view.CustomDialog;

public abstract class BaseActivity extends FragmentActivity implements OnHandlerResponse {
	private CustomDialog progressDialog;
	private String identifier;// activity的唯一标识,ActiviyManager使用

	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		identifier = this.getClass().getSimpleName() + this.hashCode() + System.currentTimeMillis();
		progressDialog = new CustomDialog(this);
	}

	public String getIdentifier() {
		return identifier;
	}

	public <T extends View> T findView(int id) {
		return (T) super.findViewById(id);
	}

	public void toast(String str) {
		Toast.makeText(this, str, Toast.LENGTH_LONG).show();
	}

	public void handlerResponse(final int aciton, final int code, final Object... obj) {
		runOnUiThread(new Runnable() {
			public void run() {
				handlerUI(aciton, code, obj);
			}
		});
	}

	public void showProgressDialog(String str) {
		progressDialog.setMessage(str);
		if (!progressDialog.isShowing()) {
			progressDialog.show();
		}
	}

	public void dismissDialog() {
		if (progressDialog.isShowing()) {
			progressDialog.dismiss();
		}
	}

	abstract public void handlerUI(int aciton, int code, Object... obj);

	/**
	 * 隐藏输入法（根据activity当前焦点所在控件的WindowToken）
	 */
	public static void hideSoftInput(Activity activity, View editText) {
		View view;
		if (editText == null) {
			view = activity.getCurrentFocus();
		} else {
			view = editText;
		}

		if (view != null) {
			InputMethodManager inputMethod = (InputMethodManager) activity.getSystemService(Context.INPUT_METHOD_SERVICE);
			inputMethod.hideSoftInputFromWindow(view.getWindowToken(), 0);
		}
	}

	/**
	 * 显示软键盘（根据焦点所在的控件）
	 */
	public static void showSoftInput(Context context) {
		((InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE)).toggleSoftInput(InputMethodManager.SHOW_FORCED, 0);
	}

	@Override
	protected void onResume() {
		ActivityStackManager.getInstance().addActivity(this);
		super.onResume();
	}

	private CustomDialog reLoginDialog;

	public void showReLoginDialog() {
		if (App.app != null) {
			App.app.destroyTask();
		}
		if (reLoginDialog == null) {
			reLoginDialog = new CustomDialog(this);
			reLoginDialog.setCancelable(false);
		}
		if (reLoginDialog.isShowing()) {
			return;
		}
		reLoginDialog.setTitile("提示");
		reLoginDialog.setMessage("您太久没有登录了，请重新登录");
		reLoginDialog.setOnlyOneBtn("确定");
		reLoginDialog.setOnlyOneBtnOnclickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				reLoginDialog.dismiss();
				gotoLogin();
			}
		});
		reLoginDialog.show();
	}

	public void showLogoutDialog() {
		final CustomDialog dialog = new CustomDialog(this);
		dialog.setTitile("提示");
		dialog.setMessage("\n 您确定要注销该应用？              \n");
		dialog.setLeftBtn("取消");
		dialog.setRightBtn("确定");
		dialog.setLeftBtnOnclickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				dialog.dismiss();
			}
		});
		dialog.setRightBtnOnclickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				dialog.dismiss();
				showProgressDialog("正在注销，请稍候");
				LoginLogic.getInstance().logout(BaseActivity.this);
			}
		});
		dialog.show();
	}

	protected void gotoLogin() {
		Intent intent = new Intent(this, LoginActivity.class);
		intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
		startActivity(intent);
		finish();
	}
}
